
using App.Components;
using FreeSql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace App
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRazorPages();
            services.AddHttpContextAccessor();
            IFreeSql fsql = new FreeSqlBuilder()
                .UseConnectionString(DataType.Dameng, "server=home.mylb.xyz;port=25336;user id=SA;password=Wzcc0577.com;database=SA;poolsize=5")
                .UseAutoSyncStructure(true)
                .UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
                .Build();
            DAL.AppContext.IFreeSql = fsql;
            services.AddSingleton(fsql);
            services.AddFreeDbContext<App.DAL.AppContext>(options => options.UseFreeSql(fsql));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment()) app.UseDeveloperExceptionPage();

            GlobalVar.ServiceProvider = app.ApplicationServices;
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.Map("/", context =>
                {
                    context.Response.Redirect("/Login");
                    return Task.CompletedTask;
                });
                endpoints.MapAreaControllerRoute(
                     name: "Admins",
                     areaName: "Admins",
                     pattern: "Admins/{controller=Home}/{action=Index}/{id?}");

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Login}/{action=Index}");
            });
            // context.Response.WriteAsync("Test");
            //App.Components.GlobalVar.IHttpContextAccessor = app.ApplicationServices.GetService<IHttpContextAccessor>();
            //App.Components.GlobalVar.ServiceProvider = app.ApplicationServices ;
            //var f2 = f.HttpContext;
            ////var a = () as HttpContextAccessor).HttpContext.RequestServices.GetService<DAL.AppContext>();
            //var a = app.ApplicationServices;
            //var e = a.GetRequiredService<DAL.AppContext>();
            //var b = a.GetService(typeof(DAL.AppContext));
            //var c = a.GetService<DAL.AppContext>();
            //var d=a.GetRequiredService(typeof(DAL.AppContext));
            //GlobalVar.DB = a;
        }
    }
}
